<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="社团名称" prop="societyName">
        <el-input
          v-model="queryParams.societyName"
          placeholder="请输入社团名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="manageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="社团名称" align="center" prop="societyName"/>
      <el-table-column label="收费标准" align="center" prop="charge"/>
      <el-table-column label="社团描述" align="center" prop="description"/>
      <el-table-column label="社团经费" align="center" prop="expenditure"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.societyFlag=='0'"
            icon="el-icon-edit"
            @click="applyJoin(scope.row)"
          >加入社团
          </el-button>
          <el-button
            size="mini"
            type="text"
            v-else
            icon="el-icon-delete"
            @click="handleDeleteSociety(scope.row)"
          >退出社团
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
  import {listManage, getManage, delManage, addManage, updateManage} from "@/api/system/manage";
  import {addSociety, delSociety} from "@/api/system/society";

  export default {
    name: "Manage",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 社团管理表格数据
        manageList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          societyName: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询社团管理列表 */
      getList() {
        this.loading = true;
        listManage(this.queryParams).then(response => {
          this.manageList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          societyName: null,
          charge: null,
          description: null,
          expenditure: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 提交按钮 */
      applyJoin(row) {
        this.$modal.confirm('加入"' + row.societyName + '"社团,需要缴纳"'+row.charge+'元,是否确认"？').then(()=>{
          addSociety(row).then(response => {
            this.$modal.msgSuccess("添加成功");
            this.open = false;
            this.getList();
          });
        });

      },
      handleDeleteSociety(row) {
        this.$modal.confirm('是否确认退出"' + row.societyName + '"社团？不会退费!').then(function () {
          return delSociety(row.id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      }
    },

  };
</script>
